﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace _1stVATCWin
{
    public partial class Employeer : Form
    {
        public Employeer()
        {
            InitializeComponent();
        }

        private void Load_dataGridView1(string IDnhanvien)
        {
            if(IDnhanvien == "")
            {
                Config file = new Config(Application.StartupPath + "\\config.ini");
                String connectionstring = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "select IDnhanvien,hoten,gioitinh,tenchucvu,employeer,product,shopping,warranty,contact,news,statistic,provider from NHANVIEN JOIN CHUCVU ON NHANVIEN.IDchucvu = CHUCVU.IDchucvu";
                SqlCommand cmd = new SqlCommand(sql, conn);

                //C1: goi = data reader va luc celldoubleclick no se cong don du lieu
                //conn.Open();
                //SqlDataReader r = cmd.ExecuteReader();
                //while (r.Read())
                //{
                //    dataGridView1.Rows.Add(new string[] { r["publisherId"].ToString(), (string)r["publisherName"], (string)r["email"] });
                //}
                //r.Close();
                //conn.Close();

                //C2: goi = data set va luc celldoubleclick no se ko cong don du lieu
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                conn.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds, "Table NHANVIEN");
                DataTable dt = ds.Tables["Table NHANVIEN"];
                DataColumn col = new DataColumn("textgioitinh");
                col.Expression = "iif(gioitinh,'Male','Female')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textemployeer");
                col.Expression = "iif(employeer,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textproduct");
                col.Expression = "iif(product,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textshopping");
                col.Expression = "iif(shopping,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textwarranty");
                col.Expression = "iif(warranty,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textcontact");
                col.Expression = "iif(contact,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textnews");
                col.Expression = "iif(news,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textstatistic");
                col.Expression = "iif(statistic,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textprovider");
                col.Expression = "iif(provider,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = ds.Tables["Table NHANVIEN"].DefaultView;
            }
            else
            {
                Config file = new Config(Application.StartupPath + "\\config.ini");
                String connectionstring = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "select IDnhanvien,hoten,gioitinh,tenchucvu,employeer,product,shopping,warranty,contact,news,statistic from NHANVIEN JOIN CHUCVU ON NHANVIEN.IDchucvu = CHUCVU.IDchucvu where IDnhanvien = @IDnhanvien";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@IDnhanvien", IDnhanvien);

                //C1: goi = data reader va luc celldoubleclick no se cong don du lieu
                //conn.Open();
                //SqlDataReader r = cmd.ExecuteReader();
                //while (r.Read())
                //{
                //    dataGridView1.Rows.Add(new string[] { r["publisherId"].ToString(), (string)r["publisherName"], (string)r["email"] });
                //}
                //r.Close();
                //conn.Close();

                //C2: goi = data set va luc celldoubleclick no se ko cong don du lieu
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                conn.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds, "Table NHANVIEN");
                DataTable dt = ds.Tables["Table NHANVIEN"];
                DataColumn col = new DataColumn("textgioitinh");
                col.Expression = "iif(gioitinh,'Male','Female')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textemployeer");
                col.Expression = "iif(employeer,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textproduct");
                col.Expression = "iif(product,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textshopping");
                col.Expression = "iif(shopping,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textwarranty");
                col.Expression = "iif(warranty,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textcontact");
                col.Expression = "iif(contact,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textnews");
                //col.Expression = "iif(news,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textstatistic");
                col.Expression = "iif(statistic,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                col = new DataColumn("textprovider");
                col.Expression = "iif(provider,'Yes','No')";
                ds.Tables["Table NHANVIEN"].Columns.Add(col);
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = ds.Tables["Table NHANVIEN"].DefaultView;
            }
            
        }
        private void Employeer_Load(object sender, EventArgs e)
        {
            //Load_dataGridView1();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            checkBoxEmployeer.Checked = true;
            checkBoxProduct.Checked = true;
            checkBoxContact.Checked = true;
            checkBoxWarranty.Checked = true;
            checkBoxShopping_Cart.Checked = true;
            checkBoxtNews.Checked = true;
            checkBoxstatistics.Checked = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            checkBoxEmployeer.Checked = false;
            checkBoxProduct.Checked = false;
            checkBoxContact.Checked = false;
            checkBoxWarranty.Checked = false;
            checkBoxShopping_Cart.Checked = false;
            checkBoxtNews.Checked = false;
            checkBoxstatistics.Checked = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
            {
                MessageBox.Show("Bảng dữ liệu trống");
            }
            else
            {
                int i = dataGridView1.SelectedRows[0].Index;
                string id = dataGridView1.Rows[i].Cells["IDnhanvien"].Value.ToString();

                Config file = new Config(Application.StartupPath + "\\config.ini");
                String connectionString = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
                SqlConnection conn = new SqlConnection(connectionString);
                string sql = "delete from NHANVIEN where IDnhanvien = @IDnhanvien";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@IDnhanvien", id);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                dataGridView1.Rows.RemoveAt(i);
            }
        }
        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
                deleteToolStripMenuItem.Enabled = false;
        }

        private void buttonAdd_Click_1(object sender, EventArgs e)
        {
            if (textBoxUserID.Text == "" && textBoxFullName.Text == "" && textBoxPass.Text == "" && textBoxConfirm.Text == ""&&textBoxAdress.Text ==""&&textBoxEmail.Text == ""&&textBoxIDnumber.Text == ""&&textBoxPhone.Text =="")
            {
                errorProviderUserID.SetError(textBoxUserID, "UserID Not Null");
                errorProviderFullname.SetError(textBoxFullName, "Fullname Not Null");
                errorProviderPass.SetError(textBoxPass, "Pass Not Null");
                errorProviderConfi.SetError(textBoxConfirm, "PassConfirm Not Null");
                errorProviderAdress.SetError(textBoxAdress,"Adress Not Null");
                errorProviderPhone.SetError(textBoxPhone, "Phone Not Null");
                errorProvider1.SetError(textBoxEmail,"Email Not Null");
                errorProvider1.SetError(textBoxIDnumber,"IDnumber Not Null");
            }
            else
            {
                errorProviderUserID.Clear();
                errorProviderFullname.Clear();
                errorProviderPass.Clear();
                errorProviderConfi.Clear();
                if (textBoxUserID.Text == "")
                {
                    errorProviderUserID.SetError(textBoxUserID, "UserID Not Null");
                }
                else
                {
                    if (textBoxFullName.Text == "")
                    {
                        errorProviderFullname.SetError(textBoxFullName, "Password Not Null");
                    }
                    else
                    {
                        if (textBoxPass.Text == "")
                        {
                            errorProviderPass.SetError(textBoxPass, "Pass Not Null");
                        }
                        else
                        {
                            if (textBoxConfirm.Text == "")
                            {
                                errorProviderConfi.SetError(textBoxConfirm, "PassConfirm Not Null");
                            }
                            else
                            {
                                    if(textBoxAdress.Text =="")
                                    {
                                        errorProviderAdress.SetError(textBoxAdress,"Adress Not Null");
                                    }
                                    else
                                    {
                                        if(textBoxPhone.Text =="")
                                        {
                                            errorProviderPhone.SetError(textBoxPhone, "Phone Not Null");
                                        }
                                        else
                                        {
                                            if(textBoxIDnumber.Text=="")
                                            {
                                                errorProvider1.SetError(textBoxIDnumber, "IDnumber Not Null");
                                            }
                                            else
                                            {
                                                if(textBoxEmail.Text =="")
                                                {
                                                    errorProvider1.SetError(textBoxEmail, "Email Not Null");
                                                }
                                                else
                                                {
                                                    if (Regex.IsMatch(textBoxEmail.Text, @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$") == false)
                                                    {
                                                        errorProvider1.SetError(textBoxEmail,"Mail Nhap sai dinh dang");
                                                    }
                                                    else
                                                    {
                                                        if (textBoxPass.Text == textBoxConfirm.Text)
                                                        {
                                                            this.DialogResult = DialogResult.OK;
                                                            // khoi tao doi tuong de doc file config.ini
                                                            Config file = new Config(Application.StartupPath + "\\Config.ini");
                                                            String connectionstring = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
                                                            SqlConnection conn = new SqlConnection(connectionstring);
                                                            string sql = "select * from NHANVIEN where IDnhanvien = @IDnhanvien";
                                                            SqlCommand cmd = new SqlCommand(sql, conn);
                                                            conn.Open();
                                                            cmd.Parameters.AddWithValue("@IDnhanvien", textBoxUserID.Text);
                                                            SqlDataReader reader = cmd.ExecuteReader();
                                                            if (reader.Read())
                                                            {
                                                                errorProviderUserID.SetError(textBoxUserID, "ID Nay co roi");
                                                            }
                                                            else
                                                            {
                                                                reader.Close();
                                                                cmd.CommandText = "insert into NHANVIEN(IDnhanvien,matkhau,hoten,ngaysinh,gioitinh,diachi,sodt,email,CMND,IDchucvu,employeer,product,shopping,warranty,contact,news,statistic,provider) values(@IDnhanvien,@matkhau,@hoten,@ngaysinh,@gioitinh,@diachi,@sodt,@email,@CMND,@IDchucvu,@employeer,@product,@shopping,@warranty,@contact,@news,@statistic,@provider)";
                                                                cmd.Parameters.Clear();
                                                                cmd.Parameters.AddWithValue("@IDnhanvien", textBoxUserID.Text);
                                                                cmd.Parameters.AddWithValue("@matkhau", textBoxPass.Text);
                                                                cmd.Parameters.AddWithValue("@hoten", textBoxFullName.Text);
                                                                cmd.Parameters.AddWithValue("@ngaysinh", dateTimePicker1.Value);  
                                                                if (radioButtonMale.Checked)
                                                                {
                                                                    cmd.Parameters.AddWithValue("@gioitinh", "True");
                                                                }
                                                                else
                                                                {
                                                                    cmd.Parameters.AddWithValue("@gioitinh", "False");
                                                                }
                                                                cmd.Parameters.AddWithValue("@diachi", textBoxAdress.Text);
                                                                cmd.Parameters.AddWithValue("@sodt", textBoxPhone.Text);
                                                                cmd.Parameters.AddWithValue("@email", textBoxEmail.Text);
                                                                cmd.Parameters.AddWithValue("@CMND", textBoxIDnumber.Text);
                                                                if (radioAdmin.Checked)
                                                                {
                                                                    cmd.Parameters.AddWithValue("@IDchucvu", "AD");
                                                                }
                                                                else
                                                                {
                                                                    if (radioProduct.Checked)
                                                                    {
                                                                        cmd.Parameters.AddWithValue("@IDchucvu", "MPR");
                                                                    }
                                                                    else
                                                                    {
                                                                        if (radioNews.Checked)
                                                                        {
                                                                            cmd.Parameters.AddWithValue("@IDchucvu", "MNE");
                                                                        }
                                                                        else
                                                                        {
                                                                            if (radioContact.Checked)
                                                                            {
                                                                                cmd.Parameters.AddWithValue("@IDchucvu", "MCO");
                                                                            }
                                                                            else
                                                                            {
                                                                                if (radioWarranty.Checked)
                                                                                {
                                                                                    cmd.Parameters.AddWithValue("@IDchucvu", "MWA");
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (radioButtonShopping_Cart.Checked)
                                                                                    {
                                                                                        cmd.Parameters.AddWithValue("@IDchucvu", "MSH");
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                        cmd.Parameters.AddWithValue("@statistic", "MST");     
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                                if (checkBoxEmployeer.Checked)
                                                                {
                                                                    cmd.Parameters.AddWithValue("@employeer", "true");
                                                                }
                                                                else
                                                                {
                                                                    cmd.Parameters.AddWithValue("@employeer", "false");
                                                                }
                                                                if (checkBoxProduct.Checked)
                                                                {
                                                                    cmd.Parameters.AddWithValue("@product", "true");
                                                                }
                                                                else
                                                                {
                                                                    cmd.Parameters.AddWithValue("@product", "false");
                                                                }
                                                                if (checkBoxShopping_Cart.Checked)
                                                                {
                                                                    cmd.Parameters.AddWithValue("@shopping", "true");
                                                                }
                                                                else
                                                                {
                                                                    cmd.Parameters.AddWithValue("@shopping", "false");
                                                                }
                                                                if (checkBoxWarranty.Checked)
                                                                {
                                                                    cmd.Parameters.AddWithValue("@warranty", "true");
                                                                }
                                                                else
                                                                {
                                                                    cmd.Parameters.AddWithValue("@warranty", "false");
                                                                }
                                                                if (checkBoxContact.Checked)
                                                                {
                                                                    cmd.Parameters.AddWithValue("@contact", "true");
                                                                }
                                                                else
                                                                {
                                                                    cmd.Parameters.AddWithValue("@contact", "false");
                                                                }
                                                                if (checkBoxtNews.Checked)
                                                                {
                                                                    cmd.Parameters.AddWithValue("@news", "true");
                                                                }
                                                                else
                                                                {
                                                                    cmd.Parameters.AddWithValue("@news", "false");
                                                                }
                                                                if(checkBoxstatistics.Checked)
                                                                {
                                                                    cmd.Parameters.AddWithValue("@statistic", "true");
                                                                }
                                                                else
                                                                {
                                                                    cmd.Parameters.AddWithValue("@statistic", "false");
                                                                }
                                                                if (checkBoxprovider.Checked)
                                                                {
                                                                    cmd.Parameters.AddWithValue("@provider", "true");
                                                                }
                                                                else
                                                                {
                                                                    cmd.Parameters.AddWithValue("@provider", "false");
                                                                }
                                                            }
                                                            cmd.ExecuteNonQuery();
                                                            conn.Close();
                                                            Load_dataGridView1("");
                                                            textBoxUserID.Text = "";
                                                            textBoxFullName.Text = "";
                                                            textBoxPass.Text = "";
                                                            textBoxConfirm.Text = "";
                                                            textBoxAdress.Text = "";
                                                            textBoxEmail.Text = "";
                                                            textBoxIDnumber.Text = "";
                                                            textBoxPhone.Text = "";
                                                            dateTimePicker1.Value = DateTime.Today;
                                                            radioButtonMale.Checked = false;
                                                            radioButtonFemale.Checked = false;
                                                            checkBoxContact.Checked = false;
                                                            checkBoxEmployeer.Checked = false;
                                                            checkBoxProduct.Checked = false;
                                                            checkBoxShopping_Cart.Checked = false;
                                                            checkBoxWarranty.Checked = false;
                                                            checkBoxstatistics.Checked = false;
                                                            checkBoxtNews.Checked = false;
                                                            radioAdmin.Checked = false;
                                                            radioButtonShopping_Cart.Checked = false;
                                                            radioContact.Checked = false;
                                                            radioNews.Checked = false;
                                                            radioProduct.Checked = false;
                                                            radioWarranty.Checked = false;
                                                            radioButtonStatistics.Checked = false;
                                                        }
                                                        else
                                                        {
                                                            errorProviderConfi.SetError(textBoxConfirm, "Pass khong trung khop");
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                            }
                        }
                    }
                }
            }
        }

        private void deleteToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
            {
                MessageBox.Show("Bảng dữ liệu trống");
            }
            else
            {
                int i = dataGridView1.SelectedRows[0].Index;
                string id = dataGridView1.Rows[i].Cells["IDnhanvien"].Value.ToString();

                Config file = new Config(Application.StartupPath + "\\config.ini");
                String connectionString = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
                SqlConnection conn = new SqlConnection(connectionString);
                string sql = "delete from NHANVIEN where IDnhanvien = @IDnhanvien";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@IDnhanvien", id);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                dataGridView1.Rows.RemoveAt(i);
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            if(radioUserID.Checked == false&& radioFullname.Checked == false)
            {
                MessageBox.Show("Check UserID or Fullname");
            }
            else
            {
                if(radioUserID.Checked==true)
                {
                    Config file = new Config(Application.StartupPath + "\\config.ini");
                    String connectionString = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
                    SqlConnection conn = new SqlConnection(connectionString);
                    string sql = "select IDnhanvien,matkhau,hoten,gioitinh,tenchucvu,employeer,product,shopping,warranty,contact,news from NHANVIEN JOIN CHUCVU ON NHANVIEN.IDchucvu = CHUCVU.IDchucvu Where IDnhanvien = @IDnhanvien";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("@IDnhanvien", textBoxSearch.Text);

                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    conn.Open();
                    DataSet ds = new DataSet();
                    adapter.Fill(ds, "Table NHANVIEN");
                    DataTable dt = ds.Tables["Table NHANVIEN"];
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = ds.Tables["Table NHANVIEN"].DefaultView;
                }
                else
                {
                    Config file = new Config(Application.StartupPath + "\\config.ini");
                    String connectionString = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
                    SqlConnection conn = new SqlConnection(connectionString);
                    string sql = "select IDnhanvien,matkhau,hoten,gioitinh,tenchucvu,employeer,product,shopping,warranty,contact,news from NHANVIEN JOIN CHUCVU ON NHANVIEN.IDchucvu = CHUCVU.IDchucvu Where hoten  like '" + textBoxSearch.Text + "%'";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("@hoten", textBoxSearch.Text);

                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    conn.Open();
                    DataSet ds = new DataSet();
                    adapter.Fill(ds, "Table NHANVIEN");
                    DataTable dt = ds.Tables["Table NHANVIEN"];
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = ds.Tables["Table NHANVIEN"].DefaultView;
                    
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {  
            Load_dataGridView1("");
        }
        private void Load_Update(string IDnhanvien)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            String connectionString = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from NHANVIEN Where IDnhanvien = @IDnhanvien";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@IDnhanvien", IDnhanvien);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if(reader.Read())
            {
                textBoxUserID.Text = (string) reader["IDnhanvien"];
                textBoxFullName.Text = (string) reader["hoten"];
                textBoxPass.Text = (string) reader["matkhau"];
                textBoxConfirm.Text = (string)reader["matkhau"];
                dateTimePicker1.Value = (DateTime)reader["ngaysinh"];
                if((bool)reader["gioitinh"]==true)
                {
                    radioButtonMale.Checked = true;
                    radioButtonFemale.Checked = false;
                }
                else
                {
                    radioButtonMale.Checked =false;
                    radioButtonFemale.Checked = true;
                }
                textBoxAdress.Text = (string) reader["diachi"];
                textBoxPhone.Text = reader["sodt"].ToString();
                textBoxEmail.Text = (string)reader["email"];
                textBoxIDnumber.Text = (string)reader["CMND"];
                if((bool)reader["employeer"]==true)
                {
                    checkBoxEmployeer.Checked = true;
                }
                else
                {
                    checkBoxEmployeer.Checked = false;
                }
                if((bool)reader["product"]==true)
                {
                    checkBoxProduct.Checked = true;
                }
                else
                {
                    checkBoxProduct.Checked = false;
                }
                if((bool)reader["shopping"]==true)
                {
                    checkBoxShopping_Cart.Checked = true;
                }
                else
                {
                    checkBoxShopping_Cart.Checked = false;
                }
                if((bool)reader["warranty"]==true)
                {
                    checkBoxWarranty.Checked = true;
                }
                else
                {
                    checkBoxWarranty.Checked = false;
                }
                if((bool)reader["contact"]==true)
                {
                    checkBoxContact.Checked = true;
                }
                else
                {
                    checkBoxContact.Checked = false;
                }
                if((bool)reader["news"]==true)
                {
                    checkBoxtNews.Checked = true;
                }
                else
                {
                    checkBoxtNews.Checked = false;
                }
                if ((bool)reader["statistic"]==true)
                {
                    checkBoxstatistics.Checked = true;
                }
                else
                {
                    checkBoxstatistics.Checked = false;
                }
                if ((bool)reader["provider"] == true)
                {
                    checkBoxprovider.Checked = true;
                }
                else
                {
                    checkBoxprovider.Checked = false;
                }
                if((string)reader["IDchucvu"]=="AD")
                {
                    radioAdmin.Checked = true;
                }
                else
                {
                    if((string)reader["IDchucvu"]=="MCO")
                    {
                        radioContact.Checked = true;
                    }
                    else
                    {
                        if((string)reader["IDchucvu"]=="MNE")
                        {
                            radioNews.Checked = true;
                        }
                        else
                        {
                            if((string)reader["IDchucvu"]=="MPR")
                            {
                                radioProduct.Checked = true;
                            }
                            else
                            {
                                if((string)reader["IDchucvu"]== "MSH")
                                {
                                    radioButtonShopping_Cart.Checked = true;
                                }
                                else
                                {
                                    if((string)reader["IDnhanvien"]=="MWA")
                                    {
                                        radioWarranty.Checked = true;
                                    }
                                    else
                                    {
                                        if((string)reader["IDnhanvien"]=="MST")
                                        {
                                            radioButtonStatistics.Checked = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("ID null");
            }
            reader.Close();
            conn.Close();
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            String IDnhanvien;
            IDnhanvien = dataGridView1.Rows[e.RowIndex].Cells["IDnhanvien"].Value.ToString();
            Load_Update(IDnhanvien);
            
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "Select * FROM NHANVIEN where IDnhanvien = @IDnhanvien";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@IDnhanvien", textBoxUserID.Text);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if(reader.Read())
            {
                reader.Close();
                cmd.CommandText = "update NHANVIEN set IDnhanvien = @IDnhanvien,hoten=@hoten,gioitinh = @gioitinh,IDchucvu = @IDchucvu,employeer = @employeer,product=@product,@shopping = @shopping,warranty = @warranty,contact = @contact,news = @news,statistic = @statistic,provider=@provider  where IDnhanvien = @IDnhanvien";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@IDnhanvien", textBoxUserID.Text);
                cmd.Parameters.AddWithValue("@hoten", textBoxFullName.Text);
                if (radioButtonMale.Checked)
                {
                    cmd.Parameters.AddWithValue("@gioitinh", "true");
                }
                else
                {
                    cmd.Parameters.AddWithValue("@gioitinh", "false");
                }
                if (radioAdmin.Checked)
                {
                    cmd.Parameters.AddWithValue("@IDchucvu", "AD");
                }
                else
                {
                    if (radioProduct.Checked)
                    {
                        cmd.Parameters.AddWithValue("@IDchucvu", "MPR");
                    }
                    else
                    {
                        if (radioNews.Checked)
                        {
                            cmd.Parameters.AddWithValue("@IDchucvu", "MNE");
                        }
                        else
                        {
                            if (radioContact.Checked)
                            {
                                cmd.Parameters.AddWithValue("@IDchucvu", "MCO");
                            }
                            else
                            {
                                if (radioWarranty.Checked)
                                {
                                    cmd.Parameters.AddWithValue("@IDchucvu", "MWA");
                                }
                                else
                                {
                                    if (radioButtonShopping_Cart.Checked)
                                    {
                                        cmd.Parameters.AddWithValue("@IDchucvu", "MSH");
                                    }
                                    else
                                    {
                                        if(radioButtonStatistics.Checked)
                                        {
                                            cmd.Parameters.AddWithValue("@statistic", "MST");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (checkBoxEmployeer.Checked)
                {
                    cmd.Parameters.AddWithValue("@employeer", "true");
                }
                else
                {
                    cmd.Parameters.AddWithValue("@employeer", "false");
                }
                if (checkBoxProduct.Checked)
                {
                    cmd.Parameters.AddWithValue("@product", "true");
                }
                else
                {
                    cmd.Parameters.AddWithValue("@product", "false");
                }
                if (checkBoxShopping_Cart.Checked)
                {
                    cmd.Parameters.AddWithValue("@shopping", "true");
                }
                else
                {
                    cmd.Parameters.AddWithValue("@shopping", "false");
                }
                if (checkBoxWarranty.Checked)
                {
                    cmd.Parameters.AddWithValue("@warranty", "true");
                }
                else
                {
                    cmd.Parameters.AddWithValue("@warranty", "false");
                }
                if (checkBoxContact.Checked)
                {
                    cmd.Parameters.AddWithValue("@contact", "true");
                }
                else
                {
                    cmd.Parameters.AddWithValue("@contact", "false");
                }
                if (checkBoxtNews.Checked)
                {
                    cmd.Parameters.AddWithValue("@news", "true");
                }
                else
                {
                    cmd.Parameters.AddWithValue("@news", "false");
                }
                if(checkBoxstatistics.Checked)
                {
                    cmd.Parameters.AddWithValue("@statistic", "true");
                }
                else
                {
                    cmd.Parameters.AddWithValue("@statistic", "false");
                }
                if (checkBoxprovider.Checked)
                {
                    cmd.Parameters.AddWithValue("@provider", "true");
                }
                else
                {
                    cmd.Parameters.AddWithValue("@provider", "false");
                }
            }
            else
            {
                MessageBox.Show("Null This UserID");
            }
            reader.Close();
            cmd.ExecuteNonQuery();
            conn.Close();
            Load_dataGridView1("");
            textBoxUserID.Text = "";
            textBoxFullName.Text = "";
            textBoxPass.Text = "";
            textBoxConfirm.Text = "";
            textBoxAdress.Text = "";
            textBoxEmail.Text = "";
            textBoxIDnumber.Text = "";
            textBoxPhone.Text = "";
            dateTimePicker1.Value = DateTime.Today;
            radioButtonMale.Checked = false;
            radioButtonFemale.Checked = false;
            checkBoxContact.Checked = false;
            checkBoxEmployeer.Checked = false;
            checkBoxProduct.Checked = false;
            checkBoxShopping_Cart.Checked = false;
            checkBoxWarranty.Checked = false;
            checkBoxstatistics.Checked = false;
            checkBoxtNews.Checked = false;
            radioAdmin.Checked = false;
            radioButtonShopping_Cart.Checked = false;
            radioContact.Checked = false;
            radioNews.Checked = false;
            radioProduct.Checked = false;
            radioWarranty.Checked = false;
            radioButtonStatistics.Checked = false;
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
